package com.gopay.bis.ccb.bcdl.operation;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang3.StringUtils;

import com.gopay.bis.common.trace.PaymentLog;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BankLogConstants;
import com.gopay.common.constants.bank.BisBankInterfaceChannel;
import com.gopay.common.constants.bank.CcbConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BisBankReturnVo;
import com.gopay.common.domain.bank.ccb.bcdl.BaseReqBody;
import com.gopay.common.domain.bank.ccb.bcdl.query.res.TransResQueryBody;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.DateUtils;

public class CcbBcdlQueryHelper extends CcbBcdlBaseHelper<TransResQueryBody> {
	
	//kafka开关
    private String kafkaSwitch;
	
	public TransResQueryBody query(BaseReqBody req, SocketTransport bcdlSocketTransport) throws GopayException{
		if(BankConstants.DCBL_CORP==req.getDcblType()){
			req.setTxCode("6W0600");
		}else if (BankConstants.DCBL_PERSON==req.getDcblType()) {
			req.setTxCode("6W1503");
		}else{
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6049);
		}
		String xml = getXml(req);
		logger.error("CcbBcdlQueryHelper query xml = "+xml);
		bcdlSocketTransport.setHost(req.getUrl());
		String resXml = (String) bcdlSocketTransport.submit(xml);
		logger.error("CcbBcdlQueryHelper query resXml = "+resXml);
		if(StringUtils.isNotBlank(resXml)){
			TransResQueryBody body = parse(resXml);
			saveQueryKafkaInfo(req,body,resXml);
			if(body!=null){
				if(!CcbConstants.BCDL_RESULT_CODE_SUCC.equals(body.getReturnCode())){
					logger.error("CcbBcdlQueryHelper error orderId = " + req.getRequestSn()+ "rspcod =" + body.getReturnCode() +", returnMsg ="+ body.getReturnMsg());
				}
				return body;
			}else{
				logger.error("CcbBcdlQueryHelper parse error orderId = "+req.getRequestSn());
			}
		}else{
			logger.error("CcbBcdlQueryHelper resXml is null orderId = "+req.getRequestSn());
		}
		
		throw new GopayException(ProcCodeConstants.PROC_CODE_100E6049);
	}

	/**
	 * 将付款查询返回的报文记录到kafka中
	 * @param req
	 * @param body
	 * @param resXml
	 */
	private void saveQueryKafkaInfo(BaseReqBody baseMsg, TransResQueryBody body, String plainText) {
		try {
			BisBankReturnVo vo = new BisBankReturnVo();
			//国付宝订单号
			vo.setGopayOrderId(baseMsg.getOrderId());
			//银行返回订单号
			vo.setBankOrderId(baseMsg.getBankOrderId());
			//银行渠道
			vo.setBankChannel(BisBankInterfaceChannel._00.value);
			//银行编码
			if(BankConstants.DCBL_CORP==baseMsg.getDcblType()){
				//对公查询
				vo.setBankCode(BankLogConstants.BANK_LOG_BCDL_C_QUERY_CCB);
			}else{
				//对私查询
				vo.setBankCode(BankLogConstants.BANK_LOG_BCDL_P_QUERY_CCB);
			}
			//银行返回错误的xml文件信息
			vo.setBankReturnXml(plainText);
			if(body!=null){
				//指令状态
				vo.setRsCode(body.getReturnCode()); 
				//银行返回的错误信息
				vo.setRsMsg(body.getReturnMsg()); 
				if(body.getInfo()!=null){
					//后续逻辑会用到
					vo.setRemark(body.getInfo().getDealResult());
					//返回信息
					vo.setRsMsg(body.getInfo().getMessage()); 
				}
			}
			//银行报文订单返回时间
			vo.setBankReturnDate(DateUtils.getCurrDate("yyyy-MM-dd HH:mm:ss"));
			//报文返回记录日志时间  时间格式：yyyy-MM-dd HH:mm:ss
			vo.setCreateTsErr(DateUtils.getCurrDate("yyyy-MM-dd HH:mm:ss"));
			//保存到kafka中
			PaymentLog.info(vo,kafkaSwitch);
		} catch (Exception e) {
			logger.error("建行付款查询返回的报文记录到kafka异常："+e);
		}
		
	}

	public String getKafkaSwitch() {
		return kafkaSwitch;
	}

	public void setKafkaSwitch(String kafkaSwitch) {
		this.kafkaSwitch = kafkaSwitch;
	}
	
	

}
